// pages/mine/trips.js
var config = require('../../config.js');
var util = require('../../network_util.js');
var commonjs = require('../../common.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    orders:{},
    orderStatusList : app.globalData.orderStatus,
    tripTypeList: app.globalData.tripType,
    orderState: 0,
    page: 1,
    notAvailable:false
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    
  },
  loadData: function(mode){
    var that = this;
    var url = config.getMySchedule + "?uId=" + that.data.userInfo.id + "&pageNo=" + this.data.page + "&pageSize=20";
    // console.log(url);
    util._get(url,
      function (res) {
        console.log("trips url = " + url);
        if (res.statusCode == 200) { //网络正常
          if (res.data.code == 200) { //数据接口正常
            // var orders = res.data.data[that.data.orderState].data;
            var orders = res.data.data;
            
          
            // console.log(orders[0].data.length);
            // console.log(orders);
            orders[0].data.map(function (order) {
              var tm = new Date(order.upTime);
              that.setData({ notAvailable: true })
              order.upTime = tm.toLocaleDateString().replace(/\//g, "-") + " " + tm.toTimeString().substr(0, 5);
              var ctm = new Date(order.createTime);
              order.createTime = ctm.toLocaleDateString().replace(/\//g, "-") + " " + ctm.toTimeString().substr(0, 5);
            });
            orders[1].data.map(function (order) {
              that.setData({ notAvailable: true })
              var tm = new Date(order.upTime);
              order.upTime = tm.toLocaleDateString().replace(/\//g, "-") + " " + tm.toTimeString().substr(0, 5);
              var ctm = new Date(order.createTime);
              order.createTime = ctm.toLocaleDateString().replace(/\//g, "-") + " " + ctm.toTimeString().substr(0, 5);
            });
            // for(var i = 0;i < orders.length; i++)
            // {
            //   // 整理数据
            //   var tm = new Date(orders[i].upTime);
            //   orders[i].upTime = tm.toLocaleDateString().replace(/\//g, "-") + " " + tm.toTimeString().substr(0, 5);
            // }
            if (mode == 1 && that.data.page > 1) {
              // console.log(orders[1].data);
              var tmp = that.data.orders;
              for (var i = 0; i < orders[1].data.length; i++) {
                tmp[1].data.push(orders[1].data[i]);
              }
              // console.log(tmp);
              that.setData({ orders: tmp });
            
            } else {
              console.log("重新初始化列表")
              that.setData({ orders: orders });
              console.log(orders)
             
            }
          }
        }
        // console.log(res);
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({ userInfo: wx.getStorageSync("userInfo") });
    console.log(this.data.userInfo);
    if (this.data.userInfo == null || this.data.userInfo == "") {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }
    this.setData({ page: 1 });
    this.loadData(0);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("Pull......");
    this.setData({ page: 0 });
    this.loadData(0);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({ page: this.data.page + 1 });
    console.log("begin onReachBottom: " + this.data.page);
    this.loadData(1);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  go2Order: function(e){
    wx.navigateTo({
      url: '../placing/order?orderId=' + e.currentTarget.dataset.oid,
    })
  },
  onSwitch: function(e){
      this.setData({orderState: e.currentTarget.dataset.state});
  }
})